/*
 * @Author: byron
 * @Date: 2021-11-05 16:12:03
 * @LastEditTime: 2022-02-06 14:13:29
 */
import {
    getHotTag,
    getTag
} from '@/api/tag.js'

const state = {
    tagList: sessionStorage.getItem('TAG_LIST') != null ?
        JSON.parse(sessionStorage.getItem('TAG_LIST')) : [],
    tagHotList: sessionStorage.getItem('TAG_HOT_LIST') != null ?
        JSON.parse(sessionStorage.getItem('TAG_HOT_LIST')) : [],
    tagNum: sessionStorage.getItem('TAG_NUM') != null ?
        sessionStorage.getItem('TAG_NUM') : 0,
}

// 同步方法
const mutations = {
    SET_TAG_NUM(state, tagNum) {
        console.log('Biaoji', );
        if (tagNum) {
            state.tagNum = tagNum
            sessionStorage.setItem('TAG_NUM', tagNum)
        } else {
            state.tagNum = 0
            sessionStorage.removeItem('TAG_NUM')
        }
    },
    SET_HOT_TAG_LIST(state, tagHotList) {
        if (tagHotList) {
            state.tagHotList = tagHotList
            sessionStorage.setItem('TAG_HOT_LIST', JSON.stringify(tagHotList))
        } else {
            state.tagHotList = []
            sessionStorage.removeItem('TAG_HOT_LIST')
        }
    },
    SET_TAG_LIST(state, tagList) {
        if (tagList.length !== 0) {
            state.tagList = tagList
            sessionStorage.setItem('TAG_LIST', JSON.stringify(tagList))
        } else {
            state.tagList = []
            sessionStorage.removeItem('TAG_LIST')
        }
    },
}

// 异步方法
const actions = {
    async getHotTag({
        commit
    }) {
        try {
            const response = await getHotTag()
            const {
                code,
                msg,
                data
            } = response
            if (code === 200) {
                commit('SET_HOT_TAG_LIST', data.map((e) => {
                    return {
                        name: e.tagName,
                        id: e.id
                    }
                }))
                return Promise.resolve()
            } else {
                return Promise.reject(msg)
            }
        } catch (e) {
            console.log(e)
        }
    },
    async getTag({
        commit
    }) {
        try {
            const response = await getTag()
            const {
                code,
                data,
                msg
            } = response
            if (code === 200) {
                const total = data.length
                const result = data.map((e) => {
                    return {
                        id: e.id,
                        name: e.tagName
                    }
                })
                result.unshift({
                    id: 0,
                    name: '全部'
                })
                console.log('王灿', result);
                commit('SET_TAG_NUM', total)
                commit('SET_TAG_LIST', result)
                return Promise.resolve()
            } else {
                return Promise.reject(msg)
            }
        } catch (e) {
            console.log(e)
        }
    },
}

export default {
    namespaced: true,
    mutations,
    actions,
    state,
}